package cn.edu.ecut.proxy3;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

public class Test1 {

	public static void main(String[] args) throws Exception {
		
		final Object target = new Smartisan();
		
		Class<?> c = Class.forName( "cn.edu.ecut.proxy3.Smartisan" );
		
		ClassLoader loader = c.getClassLoader() ; //获得加载c类的那个【类加载器】
		
		Class<?>[] interfaces = c.getInterfaces(); // 获取c类所直接实现的接口
		System.out.println( Arrays.toString( interfaces ) );
		
		InvocationHandler h = new InvocationHandler() {
			@Override
			public Object invoke(Object p, Method m, Object[] args) throws Throwable {
				System.out.println( m.getName() );
				Object value = m.invoke( target , args ) ;
				return value;
			}
		};
		
		Object proxy = Proxy.newProxyInstance( loader , interfaces , h );
		System.out.println( proxy.toString() );
		
		// 通过代理对象来获得动态代理类
		Class<?> proxyClass = proxy.getClass(); 
		System.out.println( proxyClass );
		System.out.println( proxyClass.getSuperclass() );
		
		System.out.println( proxy instanceof Smartisan ); // false
		
		if( proxy instanceof Smartisan ) {
			Smartisan s = (Smartisan) proxy ;
			s.produce();
		}

	}

}
